// 测试任务相关接口
import request from "@/utils/request";
import { GET_TOKEN } from "@/utils/token";
import useUserStore from "@/store/modules/user";
import {
  addRequirementFrom,
  addRequirementData,
  updateRequirementForm,
  updateRequirementData,
  getRequirementForm,
  getRequirementData,
  getRequirementCaseForm,
  getRequirementCaseData,
  putEditeCaseByidData,
} from "./type";
const userStore = useUserStore();

//* 测试管理
enum Test {
  GetByTestList = "/item/getByTestList", // 根据关键词搜索测试用例
  GetTestCase = "/item/getTestCase", // 获取测试拥有的用例
  GetTestList = "/item/getTestList", //获取测试人员列表
}

// 根据关键词查询所有的测试用例
export const getByTestList = (data: any) =>
  request.post<number>(Test.GetByTestList, data, {
    headers: { token: GET_TOKEN() },
  });

// 获取测试拥有的用例
export const getTestCase = () => {
  return request({
    method: "GET",
    url: Test.GetTestCase,
    params: {
      id: userStore.userInfo.ID,
    },
    headers: {
      token: GET_TOKEN(),
    },
  });
};

// 获取所有测试人员列表
export const getTestList = () => request.get(Test.GetTestList);

//* 需求管理
// 需求管理相关接口
enum Demands {
  GetItemList = "/item/getItemList", //获取所有项目下拉列表
  GetWikiTypeList = "/wiki/getWikiTypeList", //获取所有wiki类型下拉列表
  GetWikiTypeById = "/wiki/getWikiTypeById/", //根据id获取wiki类型
  GetWikiList = "/wiki/getWikiList", //获取所有wiki列表
  GetWikiMsg = "/wiki/getWikiMsg/", // 根据id获取wiki详细信息
  EditWikiMsg = "/wiki/editWikiMsg", // 更新wiki详细信息
  PutWikiArchiving = "/wiki/putWikiArchiving", // 归档一条wiki
  PostDemandsList = "/item/getRequirementRequirements", // 根据用户和条件获取所有需求并分页
  GetRequirementsNumber = "/item/getRequirementsNumber", //获取当月需求汇总数据
  PostAddRequirement = "/item/addRequirement", //添加或修改需求
  PostAddWIki = "/wiki/addWiki", //添加或修改wiki
  DelWiki = "/wiki/deleteWikiMsg", //删除wiki
  DelRequirement = "/item/deleteRequirement", //删除需求
  PutRequirement = "/item/updateRequirement", //修改需求
  PostRequirement = "/item/getRequirement", //查看需求
  GetRequirementCase = "/item/getRequirementCasePageListById", // 根据需求ID获取所有测试用例
  PostRequirementCase = "/item/byRequirementIdimportCase", // 根据需求ID导入用例
  PostUploadCaseFile = "/file/upload_file", //上传用例文件
}

// 获取所有项目下拉列表
export const getItemList = (data: any) =>
  request.get(Demands.GetItemList, data);

// 获取所有wiki类型下拉列表
export const getWikiList = (data: any) =>
  request.post(Demands.GetWikiList, data);
// 更新一条wiki消息
export const editWikiMsg = (data: any) =>
  request.put(Demands.EditWikiMsg, data);
// 归档一条wiki
export const putWikiArchiving = (data: any) =>
  request.put(Demands.PutWikiArchiving, data);

// 获取所有wiki类型列表
export const getWikiTypeList = () =>
  request.get(Demands.GetWikiTypeList);
// 根据id获取wiki类型
export const getWikiTypeById = (id: any) =>
  request.get(Demands.GetWikiTypeById + id);

// 根据用户和条件获取所有需求并分页
export const postDemandsList = (data: any) =>
  request.post(Demands.PostDemandsList, data);

// 获取当月需求汇总数据
export const getRequirementsNumber = () =>
  request.get(Demands.GetRequirementsNumber);

// 添加需求
export const postAddRequirement = (data: addRequirementFrom) =>
  request.post<addRequirementData>(Demands.PostAddRequirement, data);
// 添加WIKI
export const postAddWIki = (data: any) =>
  request.post<any>(Demands.PostAddWIki, data);

// 查看一条的wiki信息
export const getWikiMsg = (id: any) =>
  request.get(Demands.GetWikiMsg + id);

// 删除wiki
export const delWiki = (id: number) =>
  request.delete(`${Demands.DelWiki}/${id}`);


// 删除需求
export const deleteRequirement = (id: number) =>
  request.delete(`${Demands.DelRequirement}/${id}`);

// 修改需求
export const updateRequirement = (data: updateRequirementForm) =>
  request.put<updateRequirementData>(Demands.PutRequirement, data);

// 查看需求
export const postRequirement = (id: getRequirementForm) =>
  request.post<getRequirementData>(Demands.PostRequirement, id);

//* 用例管理
enum Cases {
  GetCasesScoreNumber = "/item/getCasesScoreNumber", // 当月测试用例总条数及总分数
  GetRequirementCase = "/item/getRequirementCasePageListById", // 根据需求ID获取所有测试用例
  PostRequirementCase = "/item/byRequirementIdimportCase", // 根据需求ID导入用例
  PostUploadCaseFile = "/file/upload_file", //上传用例文件
  DelCaseByid = "/case/deleteCaseByid", //删除用例
  PostAuditCaseByid = "/case/auditCaseByid", //审核用例
  PutEditeCaseByid = "/case/editeCaseByid", //修改用例
}

// 当月测试用例总条数及总分数
export const getCasesScoreNumber = () => request.get(Cases.GetCasesScoreNumber);

// 根据需求ID获取所有测试用例
export const getRequirementCase = (data: getRequirementCaseForm) =>
  request.post<getRequirementCaseData>(Cases.GetRequirementCase, data);

// 根据需求ID导入用例
export const postRequirementCase = (data: any) =>
  request.post(Cases.PostRequirementCase, data);

// 导入用例文件
/* export const postUploadCaseFile = (data: any) => {
  request.post(Cases.PostUploadCaseFile, data);
}; */
export const postUploadCaseFile = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  formData.append("fileType", "CaseFile");
  // formData.append("fileTypeId", "0");
  return request.post(Cases.PostUploadCaseFile, formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 删除用例
export const delCaseByid = (id: number) =>
  request.delete(`${Cases.DelCaseByid}/${id}`);

// 审核用例
export const postAuditCaseByid = (data: any) =>
  request.post(Cases.PostAuditCaseByid, data);

// 修改用例
export const putEditeCaseByid = (data: any) =>
  request.put<putEditeCaseByidData>(Cases.PutEditeCaseByid, data);



//* BUG管理
// BUG管理相关接口
enum Demands {
  GetRequirementsBugs = "/item/getRequirementsBugs", //获取所有需求的bug
  AddRequirementBug = "/bug/addRequirementBug",//根据需求添加bug
  FindBugMsgById = "/bug/findBugMsgById",//根据id获取bug详情
  AddBugMsgById = "/bug/addBugMsgById",//根据bugid给bug添加详情
}

// 获取所有需求的bug列表
export const getBugList = () =>
  request.get(Demands.GetRequirementsBugs);

// 添加一个bug
export const addRequirementBug = (data: any) =>
  request.post<any>(Demands.AddRequirementBug, data)

//根据id获取bug详情
export const findBugMsgById = (id: number) =>
  request.get(`${Demands.FindBugMsgById}/${id}`)

//根据bugid给bug添加详情
export const addBugMsgById = (data: any) =>
  request.post(Demands.AddBugMsgById, data)

